﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_ControlDoiMatKhauNPP : System.Web.UI.UserControl
{

    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetInfo(Session["UserType"].ToString(), Session["user"].ToString());
        }
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }

    void GetInfo(string Usertype, string UserName)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_nameUserOrBA", CommandType.StoredProcedure,
                new SqlParameter("@Usertype", Usertype),
                new SqlParameter("@Username", UserName));
            txtMaSo.Text = db.Rows[0]["UserName"].ToString();
            txtName.Text = db.Rows[0]["HoTen"].ToString();

        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            int i = dt.ExecuteSQl_int(str, "k_updatePassUserOrBa", CommandType.StoredProcedure, ref error,
                new SqlParameter("@PassHint", txtNewPass.Text),
                new SqlParameter("@Username", Session["user"].ToString()),
                new SqlParameter("@Pass", dt.GetMd5Sum(txtNewPass.Text)));
            if (i == 1)
            {
                ShowMessageBox("Cập nhật mật khẩu thành công");
            }
            else
            {
                ShowMessageBox("Hệ thống đang bảo trì");
            }
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    } 
    

}